package com.fjrcloud.aop;

import com.fjrcloud.service.UserService;
import com.fjrcloud.entity.User;
import com.fjrcloud.entity.UserType;
import com.fjrcloud.utils.ConfigInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by shengjian.lin on 17-2-21.
 */
@Aspect
@Component
@Order(10)
public class UserIntercept {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ConfigInfo configInfo;
    @Autowired
    private UserService userService;

    @Around("execution(public * com.fjrcloud.service.UserService.loginVerify(..))")
    public void login(ProceedingJoinPoint pjp) throws Throwable {

        UserType type = (UserType) pjp.proceed();
        request.getSession().setAttribute("user-type",type);

        User user = null;
        if(UserType.root.equals(type)){
            user = new User();
            user.setUserName(configInfo.getUsername());
            user.setType(UserType.root);
        }else{
            user = userService.get(pjp.getArgs()[0].toString());
        }

        request.getSession().setAttribute("user",user);

        pjp.proceed();
    }
}
